<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem to="/">{{ goods.name }}</XtxBreadItem>
        </Transition>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左边 -->
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 右边 -->
        <div class="spec">
          <GoodsName :goodsInfo="goods" />
          <!-- sku组件  -->
          <GoodsSku @change="changeSkus" :goodsInfo="goods" />
          <!-- 数量组件 -->
          <XtxNumbox label="数量" v-model:num="num" :max="goods.inventory" />
          <!-- 按钮组件  -->
          <XtxButton type="primary" style="margin-top: 20px" @click="addShoppingCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" type="1"></GoodsHot>
          <GoodsHot :goodsId="goods.id" type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { nextTick, ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { findGoods } from '@/api/product'
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/goods-sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import Message from '@/components/library/Message'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup() {
    const goods = useGoods()
    const changeSkus = sku => {
      console.log(sku)
      // 更新价格
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      // 记录选择后的sku，可能有数据，可能没有数据
      currSku.value = sku
    }
    // 后代注入数据
    provide('goods', goods)
    // 数量
    const num = ref(1)
    // 加入购物车
    const store = useStore()
    const currSku = ref(null)
    const addShoppingCart = () => {
      console.log(currSku.value)

      if (currSku.value && currSku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currSku.value
        const { id, name, price, mainPictures } = goods.value
        store.dispatch('cart/addShoppingCart', {
          skuId,
          id,
          name,
          attrsText,
          picture: mainPictures[0],
          price,
          nowPrice: price,
          selected: true,
          stock,
          count: num.value,
          isEffective: true
        })
        Message({ type: 'success', text: '加入购物车成功' })
      } else {
        Message({ text: '请选择完整规格' })
      }
    }
    return {
      goods,
      changeSkus,
      num,
      addShoppingCart
    }
  }
}
// 为防止setup中数据过多可以把函数操作写在外面封装
// 获取商品详情
const useGoods = () => {
  const goodsList = ref(null)
  const route = useRoute()
  // 出现路由地址商品ID发生变化，但是不会重新初始化组件
  watch(
    () => route.params.id,
    newVal => {
      if (newVal && `/product/${newVal}` === route.path) {
        findGoods(route.params.id).then(data => {
          goodsList.value = null
          // 让商品数据为null让后使用v-if的组件可以重新销毁和创建
          nextTick(() => {
            goodsList.value = data.result
          })
          console.log(data)
        })
      }
    },
    { immediate: true }
  )
  return goodsList
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
